import {
  crowdList,
  crowddel,
  updateCrowdInfo,
  crowdarticlelist,
  crowdarticledel,
  crowdupdatearticle,
  crowdtopiclist,
  deletetopic,
  crowdaddarticle,
  crowdActivityList,
  crowdActivityUpdate,
  crowdActivityDelete,
  crowdActivityAdd,
  crowdClassList,
  crowdClassRemove,
  crowdClassUpdate,
  crowdClassAdd,
} from '@/services/crowd';
import { message } from 'antd';
export default {
  namespace: 'crowd',

  state: {
    list: [],
    pagination: [],
    data: [],
    classData: [],
    crowdData: [],
    provinceData: [],
    cityData: [],
  },

  effects: {
    *fetchCrowdList({ payload }, { call, put }) {
      const response = yield call(crowdList, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'crowdList',
          payload: response.data,
        });
      }
    },
    *CrowdDel({ payload }, { call, put }) {
      const response = yield call(crowddel, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'crowdDel',
          ...payload,
        });
        message.success('删除成功');
      }
    },
    *fetchUpdateCrowdInfo({ payload }, { call, put }) {
      const response = yield call(updateCrowdInfo, payload);
      if (response && response.status === 'success') {
        message.success('修改成功');
        yield put({
          type: 'crowdUpdateInfo',
          payload: payload,
        });
      }
    },

    *fetchCrowdArticleList({ payload }, { call, put }) {
      const response = yield call(crowdarticlelist, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'crowdarticlelist',
          payload: response.data,
        });
      }
    },
    *CrowdAddArticle({ payload }, { call, put }) {
      const response = yield call(crowdaddarticle, payload);
      if (response && response.status === 'success') {
        const response_two = yield call(crowdarticlelist);
        if (response_two && response_two.status === 'success') {
          yield put({
            type: 'crowdarticlelist',
            payload: response_two.data,
          });
        }
        message.success('添加成功');
      }
    },
    *DeleteArticle({ payload }, { call, put }) {
      const response = yield call(crowdarticledel, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'crowdarticledelete',
          ...payload,
        });
        message.success('删除成功');
      }
    },
    *UpdateArticle({ payload }, { call, put }) {
      const response = yield call(crowdupdatearticle, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'crowdarticleupdate',
          ...payload,
        });
        message.success('修改成功');
      }
    },
    *UpdateArticleIsrecommend({ payload }, { call, put }) {
      const response = yield call(crowdupdatearticle, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'crowdarticleisrecommendupdate',
          ...payload,
        });
        payload.is_recommend === 1 ? message.success('推荐成功') : message.success('不推荐成功');
      }
    },
    *fetchTopicList({ payload }, { call, put }) {
      const response = yield call(crowdtopiclist, payload);
      if (response && response.status == 'success') {
        yield put({
          type: 'topiclist',
          payload: response.data,
        });
      }
    },
    *DeleteTopic({ payload }, { call, put }) {
      const response = yield call(deletetopic, payload);
      if (response && response.status == 'success') {
        yield put({
          type: 'topicdelete',
          ...payload,
        });
        message.success('删除成功');
      } else {
        message.error('删除失败');
      }
    },
    *fetchCrowdActivityList({ payload }, { call, put }) {
      const response = yield call(crowdActivityList, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'CrowdActivityList',
          payload: response.data,
        });
      }
    },
    *fetchCrowdActivityUpdate({ payload }, { call, put }) {
      const response = yield call(crowdActivityUpdate, payload);
      if (response && response.status === 'success') {
        message.success('修改成功');
        yield put({
          type: 'CrowdActivityUpdate',
          payload: payload,
        });
      }
    },
    *fetchCrowdActivityDel({ payload }, { call, put }) {
      const response = yield call(crowdActivityDelete, payload);
      if (response && response.status === 'success') {
        message.success('删除成功');
        yield put({
          type: 'crowdActivityDelete',
          payload: payload,
        });
      }
    },
    *fetchCrowdActivityAdd({ payload }, { call, put }) {
      const response = yield call(crowdActivityAdd, payload);
      if (response && response.status === 'success') {
        message.success('添加成功');
        yield put({
          type: 'crowdActivityAdd',
          payload: response.data,
        });
      }
    },

    *fetchCrowdClassList({ payload }, { call, put }) {
      const response = yield call(crowdClassList, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'CrowdClassList',
          payload: response.data,
        });
      }
    },

    *fetchCrowdClassRemove({ payload }, { call, put }) {
      const response = yield call(crowdClassRemove, payload);
      if (response && response.status === 'success') {
        const responseTwo = yield call(crowdClassList, payload);
        if (responseTwo && responseTwo.status === 'success') {
          message.success(response.data);
          yield put({
            type: 'CrowdClassList',
            payload: responseTwo.data,
          });
        }
      }
    },

    *fetchCrowdClassUpdate({ payload }, { call, put }) {
      const response = yield call(crowdClassUpdate, payload);
      if (response && response.status === 'success') {
        const responseTwo = yield call(crowdClassList, payload);
        if (responseTwo && responseTwo.status === 'success') {
          message.success(response.data);
          yield put({
            type: 'CrowdClassList',
            payload: responseTwo.data,
          });
        }
      }
    },

    *fetchCrowdClassAdd({ payload }, { call, put }) {
      const response = yield call(crowdClassAdd, payload);
      if (response && response.status === 'success') {
        const responseTwo = yield call(crowdClassList, payload);
        if (responseTwo && responseTwo.status === 'success') {
          message.success(response.data);
          yield put({
            type: 'CrowdClassList',
            payload: responseTwo.data,
          });
        }
      }
    },
  },

  reducers: {
    crowdList(state, action) {
      return {
        ...state,
        list: action.payload.list,
        pagination: action.payload.pagination,
        classData: action.payload.class,
      };
    },
    crowdDel(state, action) {
      for (let i = 0; i < state.list.length; i++) {
        if (state.list[i].id === action.id) {
          state.list.splice(i, 1);
        }
      }
      return {
        ...state,
      };
    },
    crowdUpdateInfo(state, action) {
      for (let i = 0; i < state.list.length; i++) {
        if (state.list[i].id === action.payload.id) {
          if (action.payload.headimgurl === null) {
            action.payload.headimgurl = state.list[i].headimgurl;
            state.list[i] = action.payload;
          } else {
            state.list[i] = action.payload;
          }
        }
      }
      return {
        ...state,
      };
    },
    crowdarticlelist(state, action) {
      return {
        ...state,
        list: action.payload.list,
        pagination: action.payload.pagination,
        data: action.payload.crowd_data,
      };
    },

    crowdarticledelete(state, action) {
      for (let i = 0; i < state.list.length; i++) {
        if (state.list[i].id === action.id) {
          state.list.splice(i, 1);
        }
      }
      return {
        ...state,
      };
    },

    crowdarticleupdate(state, action) {
      for (let i = 0; i < state.list.length; i++) {
        if (state.list[i].id === action.id) {
          if (action.headimgurl === undefined) {
            action.headimgurl = state.list[i].headimgurl;
            state.list[i] = action;
          } else {
            state.list[i] = action;
          }
        }
      }
      return {
        ...state,
      };
    },
    crowdarticleisrecommendupdate(state, action) {
      for (let i = 0; i < state.list.length; i++) {
        if (state.list[i].id === action.id) {
          state.list[i].is_recommend = action.is_recommend;
        }
      }

      return {
        ...state,
      };
    },
    topiclist(state, action) {
      return {
        ...state,
        list: action.payload.list,
        pagination: action.payload.pagination,
        data: action.payload.crowd_data,
      };
    },
    topicdelete(state, action) {
      for (let i = 0; i < state.list.length; i++) {
        if (state.list[i].id === action.id) {
          state.list.splice(i, 1);
        }
      }
      return {
        ...state,
      };
    },
    CrowdActivityList(state, action) {
      return {
        ...state,
        list: action.payload.list,
        pagination: action.payload.pagination,
        crowdData: action.payload.crowd,
        provinceData: action.payload.province,
        cityData: action.payload.city,
      };
    },
    CrowdActivityUpdate(state, action) {
      for (let i = 0; i < state.list.length; i++) {
        if (state.list[i].id === action.payload.id) {
          state.list[i] = action.payload;
        }
      }

      return state;
    },
    crowdActivityDelete(state, action) {
      for (let i = 0; i < state.list.length; i++) {
        if (state.list[i].id === action.payload.id) {
          state.list.splice(i, 1);
        }
      }
      return state;
    },
    crowdActivityAdd(state, action) {
      state.list.push(action.payload);
      return state;
    },

    CrowdClassList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },

    CrowdClassRemove(state, action) {
      for (let i = 0; i < state.list.list.length; i++) {
        if (state.list.list[i].key === action.payload.key) {
          state.list.list.splice(i, 1);
        } else {
          for (let l = 0; l < state.list.list[i].children.length; l++) {
            if (state.list.list[i].children[l].key === action.payload.key) {
              state.list.list[i].children.splice(i, 1);
            }
          }
        }
      }

      return {
        ...state,
      };
    },
  },
};
